package de.stefangoebel.aufgabenplaner;

import de.stefangoebel.aufgabenplaner.R.drawable;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AufgabeAdapter extends ArrayAdapter<Aufgabe> {
	private final Context context;
	private final Aufgabe[] values;

	public AufgabeAdapter(Context context, Aufgabe[] objects) {
		super(context, R.id.txt_name, objects);
		this.context = context;
		this.values = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Aufgabe task = values[position];
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.list_row, parent, false);
		
		ImageView img_category = (ImageView)rowView.findViewById(R.id.img_category);
		img_category.setImageResource(Misc.getResId("icon_category" + task.getCategory(), context, drawable.class));
		//img_category.setBackgroundResource(Misc.getResId("category" + task.getCategory(), context, drawable.class));
		
		ImageView img_priorityA = (ImageView)rowView.findViewById(R.id.img_priorityA);
		ImageView img_priorityB = (ImageView)rowView.findViewById(R.id.img_priorityB);
		
		if (task.getPriority().startsWith("A")) {
			img_priorityA.setVisibility(View.VISIBLE);
			img_priorityB.setVisibility(View.VISIBLE);
			//img_priorityB.setAlpha(1f);
		} else if (task.getPriority().startsWith("B")) {
			img_priorityA.setVisibility(View.GONE);
			img_priorityB.setVisibility(View.VISIBLE);
			//img_priorityB.setAlpha(0.5f);
		} else {
			img_priorityA.setVisibility(View.GONE);
			img_priorityB.setVisibility(View.GONE);
		}
		
		TextView textView = (TextView)rowView.findViewById(R.id.txt_name);
		textView.setText(task.getName());
		
		textView = (TextView)rowView.findViewById(R.id.txt_dauer);
		textView.setText(task.getDurationText());

		return rowView;
	}
	
	public Aufgabe getAufgabeByPosition(long position) {
		return values[(int)position];
	}

	public Aufgabe getAufgabeById(int id) {
		for(int i = 0; i < values.length; i++) {
			if (values[i].getId() == id) {
				return values[i];
			}
		}
		return null;
	}
	
	public int getPositionById(int id) {
		for(int i = 0; i < values.length; i++) {
			if (values[i].getId() == id) {
				return i;
			}
		}
		return 0;
	}
}
